﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace MyRecipeBook.Classes
{
    public class Ingredients
    {
        #region Ctor

        public Ingredients()
        {
        }

        #endregion

        #region Searches

        public Status GetAllIngredients(out List<Ingredient> ingredients)
        {
            var status = new Status {Code = StatusCode.NotImplemented, Result = StatusResult.Error};
            ingredients = new List<Ingredient>();



            return status;
        }

        public Status GetByType(int type, out List<Ingredient> ingredients)
        {
            var status = new Status {Code = StatusCode.NotImplemented, Result = StatusResult.Error};
            ingredients = new List<Ingredient>();

            return status;
        }

        public Status GetByNameAndType(string name, int type, out List<Ingredient> ingredients)
        {
            var status = new Status { Code = StatusCode.NotImplemented, Result = StatusResult.Error };
            ingredients = new List<Ingredient>();

            return status;
        }

        public void GetById(int id, out Ingredient ingredient, out List<Recipe> recipes)
        {
            ingredient = new Ingredient();
            recipes = new List<Recipe>();
        }

        public Status GetByName(string name, bool like, out List<Ingredient> ingredients)
        {
            var status = new Status {Code = StatusCode.NotImplemented, Result = StatusResult.Error};
            ingredients = new List<Ingredient>();

            try
            {

                const string sql = "SELECT id, name, description, type_id, type_name, image_uri, date_added, date_updated FROM ingredients";
                var conn = new SqlConnection(Properties.Settings.Default.MyRecipeBookDbCS);
                var comm = new SqlCommand { CommandText = sql, Connection = conn };

                conn.Open();

                var reader = comm.ExecuteReader();

                if (reader != null)
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                        }
                    }
                    else
                    {
                        return new Status { Result = StatusResult.Failed, Code = StatusCode.ConversionNotFound };
                    }

                    reader.Close();
                }
                conn.Close();
            }
            catch
            {
                return new Status { Result = StatusResult.Exception, Code = StatusCode.Unknown };
            }

            return status;
        }

        public Status GetByRecipe(int recipeId, out List<Ingredient> ingredients)
        {
            var status = new Status { Code = StatusCode.NotImplemented, Result = StatusResult.Error };
            ingredients = new List<Ingredient>();

            return status;
        }

        #endregion

        #region Add / Update / Remove

        public Status Add(string name, string description, string type, string imageUri)
        {
            var status = new Status { Code = StatusCode.NotImplemented, Result = StatusResult.Error };
           


            return status;
        }

        public Status Update(int id,string name)
        {
            var status = new Status { Code = StatusCode.NotImplemented, Result = StatusResult.Error };

            return status;
        }

        public Status Remove(int id)
        {
            var status = new Status { Code = StatusCode.NotImplemented, Result = StatusResult.Error };

            return status;
        }

        #endregion

        #region Import / Export

        public Status Import(string filename, out List<Ingredient> ingredients, out int success, out int failed)
        {
            var status = new Status { Code = StatusCode.NotImplemented, Result = StatusResult.Error };
            ingredients = new List<Ingredient>();
            success = 0;
            failed = 0;

            return status; 
        }

        public Status Export(string filename, List<Ingredient> ingredients)
        {
            var status = new Status { Code = StatusCode.NotImplemented, Result = StatusResult.Error };

            return status;
        }

        #endregion
    }

    public class Ingredient
    {
        /// <summary>
        /// unique id of the Ingredient
        /// </summary>
        public int Id { get; set; }
        
        /// <summary>
        /// The ingredient name
        /// </summary>
        public string Name { get; set; }

        public string Description { get; set;}

        public IngredientType IngredientType { get; set; }

        public string ImageUri { get; set; }

        public List<string> UsedInRecipes { get; set; }

        public DateTime Added { get; set; }

        public DateTime Updated { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }
}
